import {useRoutes} from "react-router-dom";
import { Suspense, lazy } from 'react';
import { UserOutlined, FolderOpenOutlined, MessageOutlined,SmileOutlined,AntDesignOutlined,PieChartOutlined} from '@ant-design/icons';
// import { Spin } from 'antd';
import Loading from "../components/Loading";
//菜单配置
const menuConfig = [
  {
    id:'1',
    title: '学生管理',
    icon:<UserOutlined />,
    children:[
      {
        title: '学生信息',
        key:'/home/student1',
      }
    ]
  },
    {
    id:'2',
    title: '权限管理',
    icon:<AntDesignOutlined />,
    children:[
      {
        title: '用户权限',
        key:'/home/per_mgr2',
      }
    ]
  },
    {
    id:'3',
    title: '数据分析',
    icon:<PieChartOutlined />,
    children:[
      {
        title: '饼状图',
        key:'/home/pie3',
      },
      {
        title: '饼状图',
        key:'/home/col3',
      },
    ]
  },
    {
    id:'4',
    title: '文件管理',
    icon:<FolderOpenOutlined />,
    children:[
      {
        title: '文件信息',
        key:'/home/file4',
      }
    ]
  },
    {
    id:'5',
    title: '聊天功能',
    icon:<MessageOutlined />,
    children:[
      {
        title: 'QQ',
        key:'/home/qq5',
      }
    ]
  },
  {
    id:'6',
    title: '个人中心',
    icon:<SmileOutlined/>,
    children:[
      {
        title: '个人信息',
        key:'/home/information6',
      },
      {
        title: '设置中心',
        key:'/home/personal_set6',
      },
    ]
  },
]
//路由配置
const routes = [
  {
    path: '/',
    auth:true,
    component:lazy(() => import('../view/Login'))
  },
  {
    path:'/home',
    auth:true,
    component:lazy(() => import('../view/Home')),
    children:[
      {
        path:'/home',
        auth:true,
        component:lazy(() => import('../view/Home/Welcome')),
      },
      {
        path:'/home/student1',
        auth:true,
        component:lazy(() => import('../view/Home/Student'))
      },
      {
        path:'/home/per_mgr2',
        auth:true,
        component:lazy(() => import('../view/Home/PerMgr')),
      },
      {
        path:'/home/pie3',
        auth:true,
        component:lazy(() => import('../view/Home/PieChart')),
      },
      {
        path:'/home/col3',
        auth:true,
        component:lazy(() => import('../view/Home/ColumnMe')),
      },
      {
        path:'/home/file4',
        auth:true,
        component:lazy(() => import('../view/Home/File')),
      },
      {
        path:'/home/qq5',
        auth:true,
        component:lazy(() => import('../view/Home/QQ')),
      },
      {
        path:'/home/personal_set6',
        auth:true,
        component:lazy(()=>import('../view/Home/PersonalSet')),
        children:[
        {
          path:'/home/personal_set6',
          auth:true,
          component:lazy(() => import('../view/Home/PersonalSet/Basic_Set')),
        },
        {
          path:'/home/personal_set6/safe_set',
          auth:true,
          component:lazy(() => import('../view/Home/PersonalSet/Safe_Set')),
        }
      ]
      }
    ]
  },
  {
    path:'/*',
    auth:true,
    component:lazy(() => import('../view/Error')),
    code:'all',
  }
]
//根据路径获取路由
const checkAuth = (routers, path)=>{
  for (const data of routers) {
    if (data.path===path) return data
    if (data.children) {
      const res = checkAuth(data.children, path)
      if (res) return res
    }
  }
  return null
}
// 路由处理方式
const  generateRouter = (routers) => {
  return routers.map((item) => {
    if (item.children) {
      item.children = generateRouter(item.children)
    }

    item.element = <Suspense fallback={
      <div className="example">
        <Loading flag={true}></Loading>
      </div>
    }>
      {/* 把懒加载的异步路由变成组件装载进去 */}
      <item.component/>
    </Suspense>
    return item
  })
}
const Router = () => useRoutes(generateRouter(routes))
const checkRouterAuth = (path)=>{
  let auth = checkAuth(routes,path)
  return auth
}
export{ Router,checkRouterAuth,menuConfig}